# FILE: ImageFactory.py
# DESC: Factory implementation for Image objects.

from EntityFactory import EntityFactory
from web.entity.Image import Image 
from infra.logging import logger

# TODO: add image width, height and extensions to config
IMG_MIN_WIDTH = 100
IMG_MAX_WIDTH = 500
IMG_MIN_HEIGHT = 80
IMG_MAX_HEIGHT = 500

class ImageFactory(EntityFactory):

	def __init__(self):
		EntityFactory.__init__(self)
		self.imgSrc_ = None
		self.imgWidth_ = 0
		self.imgHeight_ = 0


	def validate(self, argv):
		log = logger.getLogger()
		rv = True 
		if len(argv) != 3:
			rv = False
			self.errMsg_ = "Invalid number of arguments, expected 3, got " + str(len(argv)) + "."
		else:
			self.imgSrc_ = argv[0]
			self.imgWidth_ = argv[1]
			self.imgHeight_ = argv[2]
			if self.imgSrc_ and self.imgWidth_ in range (IMG_MIN_WIDTH, IMG_MAX_WIDTH) and self.imgHeight_ in range(IMG_MIN_HEIGHT, IMG_MAX_HEIGHT):
				rv = True
			else:
				log.debug("Image parameters differ from those defined in configuration. Image instance will not be created.")
				rv = False 
		return rv


	def doCreateEntity(self, consumer, relevanceFactor):
		image = Image(self.imgSrc_, self.imgWidth_, self.imgHeight_)
		if consumer:
			consumer.onImage(image)
		return image


	def getEntityType(self):
		return "^Image$"
